﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace agg.Classes
{
    public class Subscription
    {

        public class SubscriptionTags
        {
            public Subscription subscription { get; private set; }

            public SubscriptionTags(Subscription subscription)
            {
                this.subscription = subscription;
            }

            protected virtual void Change()
            {
                subscription.TagsChanged();
            }

            protected List<string> tagList = new List<string>();
            public IEnumerable<string> tags { get { return tagList; } }

            public void AddTag(string tag)
            {
                if (!tagList.Contains(tag))
                {
                    tagList.Add(tag);
                    Change();
                }
            }

            public void RemoveTag(string tag)
            {
                if (tagList.Contains(tag))
                {
                    tagList.Remove(tag);
                    Change();
                }
            }
        }

        public string channelUrl { get; protected set; }
        public SubscriptionController controller { get; protected set; }
        public PodCastChannel channel { get; protected set; }

        public Subscription(SubscriptionController controller, string channelUrl)
        {
            _tags = new SubscriptionTags(this);
            this.controller = controller;
            this.channelUrl = channelUrl;
        }

        public bool Loaded { get { return (channel != null); } }

        public void LoadChannel()
        {
            controller.LoadChannel(this);
        }

        public void SaveChannel()
        {
            controller.SaveChannel(this);
        }

        public void UpdateChannel()
        {
            controller.UpdateChannel(this);
        }

        protected virtual void Changed()
        {
        }

        private string _title;
        public string title {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    Changed();
                }
            }
        }


        protected virtual void TagsChanged()
        {
            Changed();
        }
        private SubscriptionTags _tags;
        public SubscriptionTags tags { get { return _tags; } }



    }
}
